
#include <cassert>
#include <iostream>
#include <memory>
#include <vector>
using namespace std;

struct Intp {
  int *val;
  Intp() { val = new int[2]; }
  ~Intp() {
    delete[] val;
    val = nullptr;
  }
};

void f1(std::shared_ptr<int> &ip) {
  shared_ptr<int> tmp = make_shared<int>(100);
  ip = tmp;
}

int main() {
  shared_ptr<int> p1{new int{0}};
  bool tag{p1};
  cout << "tag = " << tag << endl;
  shared_ptr<int> p2{p1};
  p2.reset(new int{3});
  cout << *p1 << endl;
  cout << *p2 << endl;

  shared_ptr<int> p3;
  bool tag2{p3};
  cout << "tag2 = " << tag2 << endl;
  cout << p3 << endl;
  shared_ptr<int> p4 = nullptr;
  cout << p4 << endl;

  vector<shared_ptr<int>> vec_sptr(2);
  cout << !vec_sptr[0] << "  " << !vec_sptr[1] << endl;

  vec_sptr[0] = make_shared<int>(1);
  vec_sptr[1] = make_shared<int>(2);

  cout << !vec_sptr[0] << "  " << !vec_sptr[1] << endl;

  vec_sptr.clear();
  cout << !vec_sptr[0] << "  " << !vec_sptr[1] << endl;
  cout << *vec_sptr[0] << "  " << *vec_sptr[1] << endl;
  cout << vec_sptr.size() << endl;

  vector<Intp> vec_intp(2);
  cout << vec_intp[0].val << "  " << vec_intp[1].val << endl;
  vec_intp.clear(); // 还是调用析构函数了
  cout << vec_intp[0].val << "  " << vec_intp[1].val << endl;

  shared_ptr<int> ipt;
  f1(ipt);
  cout << *ipt << endl;
  return 0;
}